from wsgiref.simple_server import make_server

from flask import Flask, render_template, request
import sql_con
from json_com import *
from sql_con import *
from read_excel import *
Asset_Number_C = ""
app = Flask(__name__)
XXX = 0
@app.route('/index',methods = ['GET','POST'])
def index():
    """
    主菜单
    :return:
    """
    write_json_all_data()
    write_json_pie('brand',file_name = "son_pie_brand")
    write_json_pie('department', file_name="son_pie_department")
    write_json_users()
    if XXX == 0:
        return render_template("log_in.html")
    else:
        return render_template("index.html")

@app.route('/index/change_password', methods=['GET', 'POST'])
def change_password():
    """
    修改密码
    :return:
    """
    global XXX
    if XXX == 0:
        return render_template("log_in.html")
    if request.method == 'GET':
        return render_template("change_password.html",x='1')
    else:
        zhang_hao = request.form.get('zhang_hao')
        mi_ma = request.form.get('mi_ma')
        xin_mi_ma = request.form.get('xin_mi_ma')
        xin_mi_ma1 = request.form.get('xin_mi_ma1')
        if sql_con.Query_users(zhang_hao,mi_ma):
            if xin_mi_ma == xin_mi_ma1:
                Chang_password(xin_mi_ma,zhang_hao,mi_ma)
                XXX = 0
                return render_template("change_password.html", y='1')
            return render_template("change_password.html", x='2')
        return render_template("change_password.html",x='0')

@app.route('/log_in',methods = ['GET','POST'])

def log_in():
    """
    登陆界面
    :return:
    """
    global XXX
    XXX = 0
    if request.method == 'GET':
        return render_template("log_in.html",x='1')
    else:
        zhang_hao = request.form.get('zhang_hao')
        mi_ma = request.form.get('mi_ma')
        if sql_con.Query_users(zhang_hao,mi_ma):
            XXX = 1
            return render_template("log_in.html",y='1')
        return render_template("log_in.html",x='0')

@app.route('/department',methods = ['GET','POST'])
def department():
    """
    部门
    :return:
    """
    if XXX == 0:
        return render_template("log_in.html")
    return render_template("department.html",x=['质量中心','机加质量部','特工事业部','特工研发部','实验组'])
@app.route('/index/total_sheet',methods = ['GET','POST'])
def total_sheet():
    """
    总表展示
    :return:
    """
    if XXX == 0:
        return render_template("log_in.html")
    return render_template("total_sheet.html")
@app.route('/index/Asset_registration',methods = ['GET','POST'])
def Asset_registration():
    """
    固定资产添加
    :return:
    """
    if XXX == 0:
        return render_template("log_in.html")
    if request.method == 'GET':
        return render_template("Asset_registration.html",x='1')
    else:
        Purchase_date = request.form.get('Purchase_date')
        Scheduled_scrap_date = request.form.get('Scheduled_scrap_date')
        if Purchase_date == "":
            Purchase_date ="0000-00-00"
        if Scheduled_scrap_date =="":
            Scheduled_scrap_date = "0000-00-00"
        create_general_sheet_item(type = request.form.get('type'),
        brand = request.form.get('brand'),
        Asset_Number = request.form.get('Asset_Number'),
        name = request.form.get('name'),
        user = request.form.get('user'),
        department = request.form.get('department'),
        Historical_user = request.form.get('Historical_user'),
        Condition_now = request.form.get('Condition_now'),
        service_number = request.form.get('service_number'),
        IP_address = request.form.get('IP_address'),
        Wired_MAC_address = request.form.get('Wired_MAC_address'),
        Wireless_MAC_address = request.form.get('Wireless_MAC_address'),
        Is_it_inventory = request.form.get('Is_it_inventory'),
        Hard_disk = request.form.get('Hard_disk'),
        Memory = request.form.get('Memory'),
        CPU = request.form.get('CPU'),
        Graphics_card = request.form.get('Graphics_card'),
        monitor = request.form.get('monitor'),
        Asset_Manager = request.form.get('Asset_Manager'),
        Upgrade_maintenance_record = request.form.get('Upgrade_maintenance_record'),
        Purchase_date = Purchase_date,
        Scheduled_scrap_date = Scheduled_scrap_date,
        address = request.form.get('address'),
        Classified_level = request.form.get('Classified_level'),
        Price = request.form.get('Price'),
        remarks = request.form.get('remarks'),
        serial_number = request.form.get('serial_number'),
        VID = request.form.get('VID'),
        PID = request.form.get('PID'),
        CD_drive = request.form.get('CD_drive'),
        operating_system = request.form.get('operating_system'))
        write_json_all_data()
        write_json_pie('brand', file_name="son_pie_brand")
        write_json_pie('department', file_name="son_pie_department")
        return render_template("Asset_registration.html")
@app.route('/index/user_management',methods = ['GET','POST'])
def user_management():
    """
    用户管理
    :return:
    """
    if XXX == 0:
        return render_template("log_in.html")
    if request.method == 'GET':
        return render_template("user_management.html")
    else:
        dict1 = request.form.to_dict()
        if len(dict1) == 0:
            return render_template("user_management.html",a = 3)
        if list(dict1.keys())[0] is None:
            return render_template("user_management.html")
        delete_user(list(dict1.keys())[0])
        write_json_users()
        return render_template("user_management.html")
@app.route('/index/add_user',methods = ['GET','POST'])
def add_user():
    """
   添加用户
    :return:
    """
    if XXX == 0:
        return render_template("log_in.html")
    if request.method == 'GET':
        return render_template("add_user.html")
    else:
        zhang_hao = request.form.get('name')
        mi_ma = request.form.get('password')
        if  create_user(zhang_hao, mi_ma) == False:
            return render_template("user_management.html",a = 2)
        write_json_users()
        return render_template("user_management.html",a = 1)
@app.route('/index/change_item_information',methods = ['GET','POST'])
def change_item_information():
    """
    改变已有固定资产信息
    :return:
    """
    if XXX == 0:
        return render_template("log_in.html")
    if request.method == 'GET':
        return render_template("change_item_information.html")
    else:
        global Asset_Number_C

        if request.form.get('type') is None:
            Asset_Number_C = request.form.get("Asset_Number_C")
            table = find_item_by_Asset_Number(Asset_Number_C)
            write_json(table, "son_find")
            return render_template("change_item_information.html")
        Purchase_date = request.form.get('Purchase_date')
        Scheduled_scrap_date = request.form.get('Scheduled_scrap_date')
        if Purchase_date == "":
            Purchase_date ="0000-00-00"
        if Scheduled_scrap_date =="":
            Scheduled_scrap_date = "0000-00-00"
        change_general_sheet_item_by_Asset_Number(Asset_Number_C,type = request.form.get('type'),
        brand = request.form.get('brand'),
        Asset_Number = request.form.get('Asset_Number'),
        name = request.form.get('name'),
        user = request.form.get('user'),
        department = request.form.get('department'),
        Historical_user = request.form.get('Historical_user'),
        Condition_now = request.form.get('Condition_now'),
        service_number = request.form.get('service_number'),
        IP_address = request.form.get('IP_address'),
        Wired_MAC_address = request.form.get('Wired_MAC_address'),
        Wireless_MAC_address = request.form.get('Wireless_MAC_address'),
        Is_it_inventory = request.form.get('Is_it_inventory'),
        Hard_disk = request.form.get('Hard_disk'),
        Memory = request.form.get('Memory'),
        CPU = request.form.get('CPU'),
        Graphics_card = request.form.get('Graphics_card'),
        monitor = request.form.get('monitor'),
        Asset_Manager = request.form.get('Asset_Manager'),
        Upgrade_maintenance_record = request.form.get('Upgrade_maintenance_record'),
        Purchase_date = Purchase_date,
        Scheduled_scrap_date = Scheduled_scrap_date,
        address = request.form.get('address'),
        Classified_level = request.form.get('Classified_level'),
        Price = request.form.get('Price'),
        remarks = request.form.get('remarks'),
        serial_number = request.form.get('serial_number'),
        VID = request.form.get('VID'),
        PID = request.form.get('PID'),
        CD_drive=request.form.get('CD_drive'),
        operating_system=request.form.get('operating_system'))
        write_json_all_data()
        write_json_pie('brand', file_name="son_pie_brand")
        write_json_pie('department', file_name="son_pie_department")
        return render_template("change_item_information.html",g=1)
@app.route('/index/upload', methods=['GET', 'POST'])
def upload_file():
    """
    上传excel文件
    :return:
    """
    if XXX == 0:
        return render_template("log_in.html")
    if request.method == 'POST':
        f = request.files['file']
        f.save(f.filename)
        create_peripheral_item_by_excel()
        create_software_item_by_excel()
        create_printer_item_by_excel()
        create__network_equipment_item_by_excel()
        create_video_item_by_excel()
        create_store_item_by_excel()
        create_computer_item_by_excel()
        create_key_item_by_excel()
        write_json_all_data()
        write_json_pie('brand', file_name="son_pie_brand")
        write_json_pie('department', file_name="son_pie_department")
        write_json_users()
        return render_template('upload.html',g=1)
    else:
        return render_template('upload.html')
@app.route('/index/department_show', methods=['GET', 'POST'])
def department_show():
    """
    按照部门查找
    """
    if XXX == 0:
        return render_template("log_in.html")
    if request.method == 'POST':
        department = request.form.get("department")
        if department is not None:
            table = show_sheet_by_thead_and_name("department",department)
            if len(table)>0:
                t = []
                k = []
                for j in range(0, len(table[0])):
                    for i in table:
                        if i[j] != "": t.append(i[j])
                    if len(t) == 1:
                        k.append(j)
                    if len(t) > 1 and t[1] == "0000-00-00":
                        k.append(j)
                    t = []
                k = sorted(k, reverse=True)
                for i in k:
                    for j in table:
                        j.pop(i)
                table[0] = change_list_by_dict(table[0], my_dic)
                write_json(table, 'son_department_show')
                return render_template('department_show.html')
        return render_template('department_show.html',g = 1)
    else:
        return render_template('department_show.html')
@app.route('/index/brand_show', methods=['GET', 'POST'])
def brand_show():
    """
    按照品牌模糊查找
    """
    if XXX == 0:
        return render_template("log_in.html")
    if request.method == 'POST':
        brand = request.form.get("brand")
        if brand is not None:
            table = show_sheet_by_thead_and_name_vague("brand", brand)
            t = []
            k = []
            if len(table)>0:
                for j in range(0, len(table[0])):
                    for i in table:
                        if i[j] != "": t.append(i[j])
                    if len(t) == 1:
                        k.append(j)
                    if len(t) > 1 and t[1] == "0000-00-00":
                        k.append(j)
                    t = []
                k = sorted(k, reverse=True)
                for i in k:
                    for j in table:
                        j.pop(i)
                table[0] = change_list_by_dict(table[0], my_dic)

                if len(table) > 0:
                    write_json(table, 'son_brand_vague_show')
                    return render_template('brand_show.html')

        return render_template('brand_show.html',g = 1)
    else:
        return render_template('brand_show.html')
if __name__ == '__main__':
    server = make_server('',5000,app)
    server.serve_forever()
    app.run(debug=True)